#!/bin/bash

set -e

if [[ -n "${RASTER_VISION_DEBUG}" ]]; then
    set -x
fi

function usage() {
    echo -n \
         "Usage: $(basename "$0") [<options>]

Publishes raster-vision-pytorch image to ECR.

Expects the RV_ECR_IMAGE env var to be set to <ecr_repo_name>:<tag_name>

Options:
--arm64 will publish the ARM64 image
"
}

if [ "${BASH_SOURCE[0]}" = "${0}" ]
then
    if [ "${1:-}" = "--help" ]
    then
        usage
        exit
    fi
    IMAGE_NAME="raster-vision-pytorch:latest"
    if [ "${1:-}" = "--arm64" ]
    then
        IMAGE_NAME="raster-vision-pytorch-arm64:latest"
    fi
    ACCOUNT_ID=$(aws sts get-caller-identity --output text --query 'Account')
    AWS_REGION="us-east-1"

    aws ecr get-login-password --region ${AWS_REGION} | docker login \
        --username AWS \
        --password-stdin ${ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com;
    docker tag ${IMAGE_NAME} \
        ${ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com/${RV_ECR_IMAGE}
    docker push \
        ${ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com/${RV_ECR_IMAGE}
fi
